package com.cfpl.admin_vegetable_service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_vegetable_service.mapper.VegetableEntryMapper;
import com.cfpl.admin_vegetable_service.service.VegetableEntryService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.VegetableEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author 14720
* @description 针对表【vegetable_entry(果蔬进场)】的数据库操作Service实现
* @createDate 2023-11-02 10:16:29
*/
@Service
public class VegetableEntryServiceImpl extends ServiceImpl<VegetableEntryMapper, VegetableEntry>
    implements VegetableEntryService {

    @Autowired
    private VegetableEntryMapper vegetableEntryMapper;

    /**
     * 条件分页查询进场记录信息
     * @param page 分页对象
     * @param condition 查询条件:果蔬名称、种植地、批次编号
     * @return
     */
    @Override
    public IPage<VegetableEntry> pageQueryVegetableEntryRecord(IPage<VegetableEntry> page, String condition) {
        LambdaQueryWrapper<VegetableEntry> queryWrapper = new LambdaQueryWrapper<>();
        //条件不为空加条件
        if (StrUtil.isNotBlank(condition)){
            queryWrapper
                    .like(VegetableEntry::getVegetableName,condition).or()
                    .like(VegetableEntry::getPlantLocation,condition).or()
                    .eq(VegetableEntry::getPurchaseBatch,condition);
        }

        return vegetableEntryMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertVegetableEntryRecord(VegetableEntry vegetableEntry) {
        int insert = vegetableEntryMapper.insert(vegetableEntry);
        return insert != 0;
    }

    /**
     * 模糊查询批次编号
     * @param fuzzyId 模糊ID
     */
    @Override
    public Map<String,String> fuzzySearchPurchaseBatchID(String fuzzyId) {
        return vegetableEntryMapper.fuzzySearchPurchaseBatchID(fuzzyId);
    }
}




